package com.qyhl.webtv.module_microvideo.shortvideo.classic.detail.live;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qyhl.webtv.basiclib.base.BaseFragment;
import com.qyhl.webtv.basiclib.utils.NetUtil;
import com.qyhl.webtv.basiclib.utils.dialog.OtherDialog;
import com.qyhl.webtv.commonlib.entity.microvideo.ClassicProgramNoticeBean;
import com.qyhl.webtv.commonlib.utils.eventbus.BusFactory;
import com.qyhl.webtv.commonlib.utils.eventbus.Event;
import com.qyhl.webtv.module_microvideo.R;
import com.qyhl.webtv.module_microvideo.R2;
import com.weavey.loading.lib.LoadingLayout;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class ClassicLiveFragment extends BaseFragment implements ClassicLiveContract.ClassicLiveView {
    @BindView(R2.id.recycle_view)
    RecyclerView recycleView;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;
    private View view;

    private String programId;

    private boolean isLive;

    private int status;

    private String title;

    private ClassicLivePresenter mPresenter;

    private List<ClassicProgramNoticeBean> mDataList = new ArrayList<>();

    private CommonAdapter<ClassicProgramNoticeBean> commonAdapter;

    public void setProgramId(String programId) {
        this.programId = programId;
    }

    public void setLive(boolean live) {
        isLive = live;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public static ClassicLiveFragment newInstance(String title, String programId, boolean isLive, int status) {
        ClassicLiveFragment fragment = new ClassicLiveFragment();
        fragment.setProgramId(programId);
        fragment.setLive(isLive);
        fragment.setStatus(status);
        fragment.setTitle(title);
        return fragment;
    }

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.micro_fragment_classic_live, null);
        }
        return view;
    }

    @Override
    protected void create() {
        loadMask.setStatus(LoadingLayout.Loading);
        mPresenter = new ClassicLivePresenter(this);

        recycleView.setLayoutManager(new LinearLayoutManager(getContext()));
        recycleView.setAdapter(commonAdapter = new CommonAdapter<ClassicProgramNoticeBean>(getContext(), R.layout.micro_item_classic_live, mDataList) {
            @Override
            protected void convert(ViewHolder holder, ClassicProgramNoticeBean bean, int position) {
//                holder.setText(R.id.num, "第" + (position + 1) + "讲");

                holder.setText(R.id.title, bean.getTitle());

                if (bean.isSelected()) {
                    holder.setImageResource(R.id.play_status, R.drawable.classic_live_on_icon);
                    holder.setTextColorRes(R.id.num, R.color.global_base);
                    holder.setTextColorRes(R.id.title, R.color.global_base);
                } else {
                    holder.setImageResource(R.id.play_status, R.drawable.classic_live_off_icon);
                    holder.setTextColorRes(R.id.num, R.color.micro_classic_title);
                    holder.setTextColorRes(R.id.title, R.color.micro_classic_title);
                }

                holder.setOnClickListener(R.id.play_menu, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        new OtherDialog.Builder(getActivity())
                                .setWidth(0.8f)
                                .setContentView(R.layout.micro_classic_live_introduction_dialog)
                                .setCancelable(true)
                                .setCanceledOnTouchOutside(true)
                                .setText(R.id.title, bean.getTitle())
                                .setScrollText(R.id.content, bean.getDesc())
                                .setDismissButton(R.id.cancel_btn)
                                .show();
                    }
                });
            }
        });

        mPresenter.getList(programId);
    }

    @Override
    protected void setListener() {
        loadMask.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadMask.setReloadButtonText("加载中...");
                mPresenter.getList(programId);
            }
        });

        commonAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                if (mDataList != null && mDataList.size() > 0 && position >= 0) {
                    for (int i = 0; i < mDataList.size(); i++) {
                        if (position == i) {
                            mDataList.get(i).setSelected(true);
                        } else {
                            mDataList.get(i).setSelected(false);
                        }
                    }
                    commonAdapter.notifyDataSetChanged();

                    BusFactory.getBus().post(new Event.ClassicNotice(mDataList.get(position)));
                }
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
    }

    @Override
    protected void onFirstVisible() {

    }

    @Override
    protected void onVisible() {

    }

    @Override
    protected void onHide() {

    }

    @Override
    public void setList(List<ClassicProgramNoticeBean> beanList) {
        loadMask.setStatus(LoadingLayout.Success);
        loadMask.setReloadButtonText("点击重试~");

        mDataList.clear();
        mDataList.addAll(beanList);


            if (status == 0) {
                if (mDataList.size() > 1 && mDataList.get(mDataList.size() - 1).getType().equals("LIVE")) {
                    mDataList.get(mDataList.size() - 1).setSelected(true);
                    BusFactory.getBus().post(new Event.ClassicNotice(mDataList.get(mDataList.size() - 1)));
                    return;
                }
            }

        commonAdapter.notifyDataSetChanged();
    }

    @Override
    public void setError(String msg) {
        loadMask.setStatus(LoadingLayout.Error);
        loadMask.setReloadButtonText("点击重试~");
        if (!NetUtil.isNetworkAvalible(getContext())) {
            loadMask.setErrorImage(R.drawable.error_network);
            loadMask.setErrorText("网络异常，请检查您的网络！");
        } else if (msg.contains("暂无")) {
            loadMask.setErrorImage(R.drawable.empty_content);
            loadMask.setErrorText(msg);
        } else {
            loadMask.setErrorImage(R.drawable.error_content);
            loadMask.setErrorText(msg);
        }
    }
}
